from flask import flash, render_template, redirect, url_for, abort
from flask_login import login_required, current_user

from . import userprint as userview
from .forms import edictInfoForm
from ..modle import TUserInformation, TUser
from .. import db


@userview.route('/user/<username>')
def user(username):
    user = TUser.query.filter_by(user_name=username).first()
    if user is None:
        abort(404)
    return render_template('user_page.html', user=user)


@userview.route('/edict/info', methods=['GET', 'POST'])
@login_required
def edict_userinfo():
    edict_info_form = edictInfoForm()
    if edict_info_form.validate_on_submit():

        # 获取邮箱手机号并验证
        user_email = edict_info_form.user_email.data
        user_phonenumber = edict_info_form.user_phonenumber.data

        user_info = TUserInformation.query.filter_by(user_email=user_email).first()
        if user_info is not None and user_info.user_id != current_user.user_id:
            flash('this email has been used!')
            return render_template('user_info_edict.html', form=edict_info_form)

        user_info = TUserInformation.query.filter_by(user_phonenumber=user_phonenumber).first()
        if user_info is not None and user_info.user_id != current_user.user_id:
            flash('this phonenumber has been used! Try the other one or pass it')
            return render_template('user_info_edict.html', form=edict_info_form)

        user_truename = edict_info_form.user_truename.data
        user_sex = edict_info_form.user_sex.data
        user_birthday = edict_info_form.user_birthday.data
        user_profession = edict_info_form.user_profession.data
        user_units = edict_info_form.user_units.data
        user_location = edict_info_form.user_location.data
        user_about = edict_info_form.user_about.data

        # 提交用户的修改
        user_info = TUserInformation.query.filter_by(user_id=current_user.user_id).first()
        user_info.user_email = user_email
        user_info.user_phonenumber = user_phonenumber
        user_info.user_truename = user_truename
        user_info.user_sex = user_sex
        user_info.user_birthday = user_birthday
        user_info.user_profession = user_profession
        user_info.user_units = user_units
        user_info.user_location = user_location
        user_info.user_about = user_about
        db.session.add(user_info)
        return redirect(url_for('main.index'))

    # 获取用户信息并填写好
    user_info = TUserInformation.query.filter_by(user_id=current_user.user_id).first()
    edict_info_form.user_email.data = user_info.user_email
    edict_info_form.user_phonenumber.data = user_info.user_phonenumber
    edict_info_form.user_truename.data = user_info.user_truename
    edict_info_form.user_sex.data = user_info.user_truename
    edict_info_form.user_birthday.data = user_info.user_birthday
    edict_info_form.user_profession.data = user_info.user_profession
    edict_info_form.user_units.data = user_info.user_units
    edict_info_form.user_location.data = user_info.user_location
    return render_template('user_info_edict.html', form=edict_info_form)
